/*
 * $Id: Axis.java,v 1.13 2003/05/31 15:22:07 znamenacek Exp $
 *
 * Copyright (c) Tomas Znamenacek
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 */

package net.sf.vofce.canvas;

import org.apache.log4j.Category;

/**
 * An axis.
 * There are two kind of units used within this class&mdash;<em>samples</em>,
 * physical discrete units (usually but not always pixels) and <em>logical
 * units</em> that represent the continuous real axis.
 * 
 * @author Tomas Znamenacek
 */
public class Axis {

	/** Default mark distance measured in samples. */
	public static final int	DEFAULT_MARK_DISTANCE = 60;

	/** Default axis position. */
	public static final int	DEFAULT_POSITION = 0;

	/** Default axis scale. */
	public static final double DEFAULT_SCALE = 1;

	/** Precision used to compare numbers. */
	public static final double PRECISION = Math.pow(10, -4);
	
	/** Logger instance. */
	protected static Category logger = Category.getRoot();

	/**
	 * Physical length of the axis.
	 */
	protected int physicalLength = 0;

	/**
	 * Distance between two marks measured in samples.
	 */
	protected int markDistance = DEFAULT_MARK_DISTANCE;
	
	/**
	 * Position of the zero mark.
	 */
	protected int position = 0;

	/**
	 * Position of the zero mark.
	 */
	protected int zeroPosition = -1;
	
	/**
	 * Logical step represented by one mark.
	 */
	protected double scale = 1;

	/**
	 * Sample size: the advance on the real axis represented by one sample.
	 */
	protected double sampleSize;
	
	/**
	 * Logical values on the axis indexed by the physical offset.
	 */
	protected double[] values = new double[physicalLength];

	/**
	 * Indexes of the marks in the <code>values</code> array.
	 */
	protected int[] marks;

	/**
	 * Creates new axis with specified physical size, leaving logical
	 * size at default values.
	 */
	public Axis(int physicalLength) {

		this.physicalLength = physicalLength;
		reset();
	}
	
	/**
	 * Creates new axis with specified physical and logical size.
	 */
	public Axis(int physicalLength, int position, double scale) {

		this.physicalLength = physicalLength;
		set(position, scale);
	}

	/** Sets new physical length. */
	public void setPhysicalLength(int physicalLength) {
		
		this.physicalLength = physicalLength;
		recalculate();
	}
	
	/**
	 * Returns logical axis length.
	 */
	public double getLogicalLength() {

		return (physicalLength / markDistance) * scale;
	}

	/**
	 * Returns physical axis length.
	 */
	public int getPhysicalLength() {

		return physicalLength;
	}

	/**
	 * Returns lower axis bound.
	 */
	public int getPosition() {

		return position;
	}

	/**
	 * Returns axis scale.
	 */
	public double getScale() {

		return scale;
	}
	
	/**
	 * Returns sampled axis values.
	 */
	public double[] getValues() {
		
		return values;
	}

	/**
	 * Returns distance between adjacent marks.
	 */
	public int getMarksDistance() {
	
		return markDistance;
	}
	
	/**
	 * Returns marks positions.
	 */
	public int[] getMarks() {

		return marks;
	}

	/**
	 * Returns zero position.
	 */
	public int getZeroPosition() {

		return zeroPosition;
	}
	
	/**
	 * Converts between physical and logical coordinates.
	 */
	public double physicalToLogical(int phys) {

		return values[phys];
	}

	/**
	 * Converts between physical and logical coordinates.
	 */
	public int logicalToPhysical(double log) {

		double lowest = (position - Math.floor(markNumber() / 2)) * scale;
		int result = (int) Math.round((log - lowest) / sampleSize);
		return result;
	}
	
	//
	// Axis manipulation
	//
	
	/** Set the axis parameters. */
	public void set(int position, double scale) {

		this.position = position;
		this.scale = scale;
		recalculate();
	}

	/** Reset the axis to its default layout. */
	public void reset() {

		this.position = DEFAULT_POSITION;
		this.scale = DEFAULT_SCALE;
		recalculate();
	}

	/** Zoom in. */
	public void zoomIn() {

		scale = scale / 2;
		recalculate();
	}

	/** Zoom out. */
	public void zoomOut() {

		scale = scale * 2;
		recalculate();
	}

	/** Move the axis by given number of marks. */
	public void move(int shift) {

		position += shift;
		recalculate();
	}
	
	/**
	 * Move the axis so that given number is approximately in
	 * the middle of the axis.
	 */
	public void moveTo(double value) {

		position = (int) Math.round(value / scale);
		recalculate();
	}
	
	//
	// PROTECTED METHODS
	// 

	/**
	 * Recalcualtes the axis layout, the heart of this class.
	 */
	protected void recalculate() {

		int m = 0;

		int firstMark = (int) (position - Math.floor(markNumber() / 2)); 
		int lastMark = firstMark + markNumber();
		
		double mark = firstMark * scale;

		sampleSize = scale / markDistance;
		marks = new int[markNumber()+1];

		zeroPosition = (firstMark * lastMark <= 0) ? -firstMark * markDistance : -1;
		
		if (values.length != physicalLength) {
			values = new double[physicalLength];
		}

		for (int i = 0; i < values.length; i++) {

			if (i % markDistance == 0) {
		
				marks[m++] = i;
				values[i] = mark;
				mark += scale;
				
			} else {

				values[i] = values[i-1] + sampleSize;
			}
		}
	}
	
	/** Returns the number of marks on the axis. */
	protected int markNumber() {

		return (int) Math.floor(physicalLength / markDistance);
	}
}
